/**
 * Copyright (c) 2010, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.propertydescriptor.definitions;

import com.acarter.propertytable.Property;
import com.acarter.propertytable.PropertySection;
import com.acarter.propertytable.PropertySectionState;
import com.acarter.propertytable.PropertyTable;
import com.acarter.propertytable.propertyobject.EnumPropertyObject;
import com.acarter.propertytable.propertyobject.StringPropertyObject;
import com.acarter.propertytable.propertyobject.EnumPropertyObject.I_EnumPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.A_PropertyPage;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.QuaternionPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.Vector3fPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.QuaternionPropertyObject.I_QuaternionPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.Vector3fPropertyObject.I_Vector3fPropertyObjectListener;
import com.ardor3d.math.Quaternion;
import com.ardor3d.math.type.ReadOnlyQuaternion;
import com.ardor3d.math.type.ReadOnlyVector3;
import com.ardor3d.renderer.queue.RenderBucketType;
import com.ardor3d.scenegraph.Spatial;
import com.ardor3d.scenegraph.hint.CullHint;
import com.ardor3d.scenegraph.hint.LightCombineMode;
import com.ardor3d.scenegraph.hint.NormalsMode;
import com.ardor3d.scenegraph.hint.TextureCombineMode;

/**
 * @author Andrew Carter
 */
public class SpatialPropertyPage extends A_PropertyPage {
	
	/**  */
	private Spatial spatial = null;
	
	/**
	 * 
	 */
	public SpatialPropertyPage() {
		
		PropertySection section = new PropertySection("Spatial");
		section.addProperty(new Property("Name", new StringPropertyObject()));
		section.addProperty(new Property("Translation", new Vector3fPropertyObject()));
		section.addProperty(new Property("Rotation", new QuaternionPropertyObject()));
		section.addProperty(new Property("Scale", new Vector3fPropertyObject()));
		section.addProperty(new Property("Cull Hint", new EnumPropertyObject<CullHint>()));
		section.addProperty(new Property("Light Combine Mode", new EnumPropertyObject<LightCombineMode>()));
		section.addProperty(new Property("Texture Combine Mode", new EnumPropertyObject<TextureCombineMode>()));
		section.addProperty(new Property("Normals Mode", new EnumPropertyObject<NormalsMode>()));
		section.addProperty(new Property("Render Bucket", new EnumPropertyObject<RenderBucketType>()));
		
		section.setState(PropertySectionState.EXPANDED);
		
		model.addPropertySection(section);
	}
	
	/**
	 * @see com.acarter.scenemonitor.propertydescriptor.A_PropertyPage#updateInfo(com.acarter.propertytable.PropertyTableModel, java.lang.Object)
	 */
	@Override
	public void updateModel(PropertyTable table, Object object) {

		if(object instanceof Spatial) {
			
			Spatial spatial = (Spatial)object;
			
			if(this.spatial == null || !this.spatial.equals(spatial)) {
				
				this.spatial = spatial;
				updateListeners(spatial);
				
				spatial.updateGeometricState(0, true);
			}
		}
	}
	
	/**
	 * 
	 * @param table
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	protected void updateListeners(final Spatial spatial) {
		
		StringPropertyObject name = (StringPropertyObject)model.getPropertySection("Spatial").getProperty("Name").getPropertyObject();
		
		name.SetListener(new StringPropertyObject.I_StringPropertyObjectListener() {
			
			public String readValue() {
				return spatial.getName();
			}

			public void saveValue(String value) {
				
				spatial.setName(value);
			}
		});
		
		Vector3fPropertyObject translation = (Vector3fPropertyObject)model.getPropertySection("Spatial").getProperty("Translation").getPropertyObject();
		
		translation.SetListener(new I_Vector3fPropertyObjectListener() {

			public ReadOnlyVector3 readValue() {
				
				return spatial.getTranslation();
			}

			public void saveValue(ReadOnlyVector3 value) {
				
				spatial.setTranslation(value);
			}
		});
		
		QuaternionPropertyObject rotation = (QuaternionPropertyObject)model.getPropertySection("Spatial").getProperty("Rotation").getPropertyObject();
		
		rotation.SetListener(new I_QuaternionPropertyObjectListener() {

			private final Quaternion quaternion = new Quaternion();
			
			public ReadOnlyQuaternion readValue() {
				
				return quaternion.fromRotationMatrix(spatial.getRotation());
			}

			public void saveValue(ReadOnlyQuaternion value) {
				
				spatial.setRotation(value);
			}
		});
		
		Vector3fPropertyObject scale = (Vector3fPropertyObject)model.getPropertySection("Spatial").getProperty("Scale").getPropertyObject();
		
		scale.SetListener(new I_Vector3fPropertyObjectListener() {

			public ReadOnlyVector3 readValue() {
				
				return spatial.getScale();
			}

			public void saveValue(ReadOnlyVector3 value) {
				
				spatial.setScale(value);
			}
		});
		
		EnumPropertyObject<CullHint> cullHint = (EnumPropertyObject<CullHint>)model.getPropertySection("Spatial").getProperty("Cull Hint").getPropertyObject();

		cullHint.SetListener(new I_EnumPropertyObjectListener<CullHint>() {
			
			public CullHint readValue() {
				
				return spatial.getSceneHints().getLocalCullHint();
			}

			public void saveValue(CullHint value) {
				
				spatial.getSceneHints().setCullHint(value);
			}
		});
		
		EnumPropertyObject<LightCombineMode> lightComb = (EnumPropertyObject<LightCombineMode>)model.getPropertySection("Spatial").getProperty("Light Combine Mode").getPropertyObject();
		
		lightComb.SetListener(new I_EnumPropertyObjectListener<LightCombineMode>() {

			public LightCombineMode readValue() {
				
				return spatial.getSceneHints().getLocalLightCombineMode();
			}

			public void saveValue(LightCombineMode value) {
				
				spatial.getSceneHints().setLightCombineMode(value);
			}
		});
		
		EnumPropertyObject<TextureCombineMode> textureComb = (EnumPropertyObject<TextureCombineMode>)model.getPropertySection("Spatial").getProperty("Texture Combine Mode").getPropertyObject();
		
		textureComb.SetListener(new I_EnumPropertyObjectListener<TextureCombineMode>() {

			public TextureCombineMode readValue() {
				
				return spatial.getSceneHints().getLocalTextureCombineMode();
			}

			public void saveValue(TextureCombineMode value) {
				
				spatial.getSceneHints().setTextureCombineMode(value);
			}
		});
		
		EnumPropertyObject<NormalsMode> normalsMode = (EnumPropertyObject<NormalsMode>)model.getPropertySection("Spatial").getProperty("Normals Mode").getPropertyObject();
		
		normalsMode.SetListener(new I_EnumPropertyObjectListener<NormalsMode>() {

			public NormalsMode readValue() {
				
				return spatial.getSceneHints().getLocalNormalsMode();
			}

			public void saveValue(NormalsMode value) {
				
				spatial.getSceneHints().setNormalsMode(value);
			}
		});
		
		EnumPropertyObject<RenderBucketType> renderBucketMode = (EnumPropertyObject<RenderBucketType>)model.getPropertySection("Spatial").getProperty("Render Bucket").getPropertyObject();
		
		renderBucketMode.SetListener(new I_EnumPropertyObjectListener<RenderBucketType>() {
			
			public RenderBucketType readValue() {
				
				return spatial.getSceneHints().getLocalRenderBucketType();
			}

			public void saveValue(RenderBucketType value) {
				spatial.getSceneHints().setRenderBucketType(value);
			}
		});
	}
}
